import { defineStore } from 'pinia'
import { ref } from 'vue'


export const useGoodsSimulationStore = defineStore('goodsSimulationList', () => {
  const goodsSimulationList = ref([])

  const getGoodsSimulationList = () => {
    return goodsSimulationList.value
  }

  const addGoodsSimulationList = (data) => {
    console.log(data)
    // goodsSimulationList.value.filter(item => item.goodsId != data.goodsId && item.customerLine != data.customerLine).push(data)
    const index = goodsSimulationList.value.findIndex(item => item.goodsId === data.goodsId && item.customerLine === data.customerLine)
    if (index !== -1) {
      // 如果找到相同的数据，先删除再添加（实现更新效果）
      goodsSimulationList.value.splice(index, 1);
    }
    goodsSimulationList.value.push(data)
    // goodsSimulationList.value.push(data)
  }


  const setGoodsSimulationList = (data) => {
    goodsSimulationList.value = data
  }

  const delGoodsSimulationList = (data) => {
    console.log(data)
    goodsSimulationList.value = goodsSimulationList.value.filter(item =>item.goodsId!=data.goodsId && item.customerLine != data.customerLine)
  }

  return {
    delGoodsSimulationList,
    goodsSimulationList,
    getGoodsSimulationList,
    addGoodsSimulationList,
    setGoodsSimulationList
  }
},
  {
    persist: true,
  }
)
